/*
 * Filename    : Entity_Benda.java
 * Projectname : RamaShinta
 * Date        : Dec 26, 2012 , 11:20:19 AM 
 * 
 */

package com.amikomgamedev.ramashinta.entity;

import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.Sprite;
import org.andengine.ui.activity.BaseGameActivity;

import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.graphic.Graphic_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;

public class Entity_Benda implements Graphic_Data
{
	public Sprite				sprBenda;
	public Sprite				sprChoco;
	public Sprite				sprRacun;
	public Sprite				sprKayu;
	public Sprite				sprApi;
	public Sprite				sprBoneka;

	private Rectangle			rectTanah;
	private Graphic_Detach		graphic;
	private BaseGameActivity	activity;
	PhysicsHandler				handler;

	final float					xLeft	= Utils.getRatio(200);
	final float					x1		= Utils.getRatio(300);
	final float					xMiddle	= Utils.getRatio(400);
	final float					x2		= Utils.getRatio(500);
	final float					xRight	= Utils.getRatio(600);

	private PoolBenda			poolBenda;
	private Sprite				sprCoba;
	private PoolBenda			poolChoco;
	private Sprite				sprCoba2;
	private PoolBenda			poolRacun;
	private Sprite				sprCoba3;
	private PoolBenda			poolKayu;
	private Sprite				sprCoba4;
	private PoolBenda			poolApi;
	private Sprite				sprCoba5;
	private PoolBenda			poolBoneka;
	private Sprite				sprCoba6;
	
	public float				gerakan;
	public float				speedUp;
	public float				speedMed;
	float						x, y;
	private int rndmSpr;
	public Sprite sprRandom;
	private int getSprlove = 0;
	private int getSprChoco = 1;
	private int getSprRacun= 2;
	private int getSprKayu = 3;
	private int getSprApi = 4;
	private int getSprBoneka = 5;
	

	public Entity_Benda(float posX, float posY, int rndSpr, Graphic_Detach pGraphic, BaseGameActivity pActivty)
	{
		activity = pActivty;
		graphic = pGraphic;
		rndmSpr = rndSpr;
		
		switch (rndmSpr)
		{
			case 0:
			{
				poolBenda = new PoolBenda(posX, posY, Utils.getRatio(ITEM_LOVE_WIDTH), Utils.getRatio(ITEM_LOVE_HEIGHT), activity,
						graphic.reg_Item_Love);
				sprCoba = poolBenda.obtainPoolItem();
				
				sprBenda = new Sprite(posX, posY, Utils.getRatio(ITEM_LOVE_WIDTH), Utils.getRatio(ITEM_LOVE_HEIGHT),
						graphic.reg_Item_Love, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBenda);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
					handler = new PhysicsHandler(sprBenda);
					sprBenda.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBenda);
				
			}
				break;
			case 1:
			{
				poolChoco = new PoolBenda(posX, posY, Utils.getRatio(ITEM_CHOCO_WIDTH), Utils.getRatio(ITEM_CHOCO_HEIGHT), activity, 
						graphic.reg_Item_Choco);
				sprCoba2 = poolChoco.obtainPoolItem();
				
				sprChoco =  new Sprite(posX, posY, Utils.getRatio(ITEM_CHOCO_WIDTH), Utils.getRatio(ITEM_CHOCO_HEIGHT),
						graphic.reg_Item_Choco, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprChoco);
				gerakan = posY;
				x = posX;
				y = posY;
				
					handler = new PhysicsHandler(sprChoco);
					sprChoco.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprChoco);
				
			}
				break;
			case 2:
			{
				poolRacun = new PoolBenda(posX, posY, Utils.getRatio(ITEM_RACUN_WIDTH), Utils.getRatio(ITEM_RACUN_HEIGHT), activity, 
						graphic.reg_Item_Racun);
				sprCoba3 = poolRacun.obtainPoolItem();
				
				sprRacun =  new Sprite(posX, posY, Utils.getRatio(ITEM_RACUN_WIDTH), Utils.getRatio(ITEM_RACUN_HEIGHT),
						graphic.reg_Item_Racun, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprRacun);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprRacun);
					sprRacun.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprRacun);
				
			}
				break;
			case 3:
			{
				poolKayu = new PoolBenda(posX, posY, Utils.getRatio(ITEM_KAYU_WIDTH), Utils.getRatio(ITEM_KAYU_HEIGHT), activity, 
						graphic.reg_Item_Kayu);
				sprCoba4 = poolKayu.obtainPoolItem();
				
				sprKayu =  new Sprite(posX, posY, Utils.getRatio(ITEM_KAYU_WIDTH), Utils.getRatio(ITEM_KAYU_HEIGHT),
						graphic.reg_Item_Kayu, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprKayu);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprKayu);
					sprKayu.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprKayu);
				
			}
				break;
			case 4:
			{
				poolApi = new PoolBenda(posX, posY, Utils.getRatio(ITEM_API_WIDTH), Utils.getRatio(ITEM_API_HEIGHT), activity, 
						graphic.reg_Item_Api);
				sprCoba5 = poolApi.obtainPoolItem();
				
				sprApi =  new Sprite(posX, posY, Utils.getRatio(ITEM_API_WIDTH), Utils.getRatio(ITEM_API_HEIGHT),
						graphic.reg_Item_Api, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprApi);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprApi);
					sprApi.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprApi);
				
			}
				break;
			case 5:
			{
				Utils.TRACE("terserah");
				poolBoneka = new PoolBenda(posX, posY, Utils.getRatio(ITEM_BONEKA_WIDTH), Utils.getRatio(ITEM_BONEKA_HEIGHT), activity, 
						graphic.reg_Item_Boneka);
				sprCoba6 = poolBoneka.obtainPoolItem();
				
				sprBoneka =  new Sprite(posX, posY, Utils.getRatio(ITEM_BONEKA_WIDTH), Utils.getRatio(ITEM_BONEKA_HEIGHT),
						graphic.reg_Item_Boneka, activity.getVertexBufferObjectManager());
				graphic.spr_Gameplay_bg.attachChild(sprBoneka);
				gerakan = posY;
				x = posX;
				y = posY;
				
				
				
					handler = new PhysicsHandler(sprBoneka);
					sprBoneka.registerUpdateHandler(handler);
					activity.getEngine().registerUpdateHandler(sprBoneka);
				
			}
				break;
		}
		

		
		getRandomSpr(rndSpr);
		
		// graphic.spr_Gameplay_bg.registerUpdateHandler(pUpdateHandler);
		
		
	}

	public Entity_Benda()
	{
		// TODO Auto-generated constructor stub
	}
	
	public Sprite getRandomSpr(int rndSpr){
		if(rndSpr==getSprlove){
			sprRandom = sprBenda;
			return sprRandom;
		}else if(rndSpr==getSprChoco){
			sprRandom = sprChoco;
			return sprRandom;
		}else if(rndSpr==getSprRacun){
			sprRandom = sprRacun;
			return sprRandom;
		}else if(rndSpr==getSprKayu){
			sprRandom = sprKayu;
			return sprRandom;
		}else if(rndSpr==getSprApi){
			sprRandom = sprApi;
			return sprRandom;
		}else{
			sprRandom = sprBoneka;
			return sprRandom;
		}
	}

	public boolean	gerakKeAtas		= true;

	int		randomPosisiX	= -1;

	public void onUpdate(float pSecondUpdate)
	{
		// System.out.println("benda h = " + sprBenda.getHeight());
		// System.out.println("gerakan = " + gerakan);
		if (gerakKeAtas)
		{
			gerakan -= 5;
			if (sprRandom.getY() < -sprRandom.getHeight())
			{
				randomPosisiX = Utils.GetRandomInRange(1, 5);
				switch (randomPosisiX)
				{
					case 1:
					{
						sprRandom.setX(xLeft);
					}
						break;
					case 2:
					{
						sprRandom.setX(x1);
					}
						break;
					case 3:
					{
						sprRandom.setX(xMiddle);
					}
						break;
					case 4:
					{
						sprRandom.setX(x2);
					}
						break;
					case 5:
					{
						sprRandom.setX(xRight);
					}
						break;
				}
				gerakKeAtas = false;
			}
		} else
		{
			gerakan = Utils.getRatio(1.25f);
			speedMed = Utils.getRatio(1);
			speedUp = Utils.getRatio(2);
		}
		
		if(sprRandom==sprRacun){
			sprRandom.setY(sprRandom.getY() + gerakan +speedMed);
		}else if(sprRandom==sprApi){
			sprRandom.setY(sprRandom.getY() + gerakan +speedMed);
		}else if(sprRandom==sprKayu){
		sprRandom.setY(sprRandom.getY() + gerakan + speedUp);
		}else if(sprRandom==sprBoneka){
			sprRandom.setY(sprRandom.getY() + gerakan + speedMed);
		}else{
			sprRandom.setY(sprRandom.getY() + gerakan);
		}
		// if (rectBenda.collidesWith(rectTanah))
		// {
		// vanishItem();
		// }
		
		
	}

	public void vanishItem()
	{
		sprBenda.detachSelf();
		sprChoco.detachSelf();
		sprRacun.detachSelf();
		sprApi.detachSelf();
		sprKayu.detachSelf();
		sprBoneka.detachSelf();
	}

}
